home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / video / fly8111-.000 / fly8111- / fly8 / player.c < prev    next >
C/C++ Source or Header  |  1979-12-31  |  3KB  |  201 lines

  1. /* --------------------------------- player.c ------------------------------- */
  2.  
  3. /* This is part of the flight simulator 'fly8'.
  4.  * Author: Eyal Lebedinsky (eyal@ise.canberra.edu.au).
  5. */
  6.  
  7. /* Manage participating players
  8. */
  9.  
  10. #include "fly.h"
  11.  
  12.  
  13. static PLAYER    *players = 0;        /* active list */
  14.  
  15. extern PLAYER * FAR
  16. player_add (PACKET *pack)
  17. {
  18.     PLAYER    *pl;
  19.  
  20.     if (!NEW (pl))
  21.         return (0);
  22.  
  23.     pl->next = players;
  24.     players = pl;
  25.     pl->netport = pack->netport;
  26.     pl->timeout = st.PlayerTimeout;
  27.     memcpy (pl->address, pack->address, LADDRESS);
  28.     PlName (pl);
  29.     return (pl);
  30. }
  31.  
  32. extern PLAYER * FAR
  33. player_delete (PLAYER *player)
  34. {
  35.     PLAYER    *pl, *pp;
  36.  
  37.     if (!player)
  38.         return (0);
  39.  
  40.     for (pp = 0, pl = players; pl; pp = pl, pl = pl->next) {
  41.         if (pl == player) {
  42.             pl = pl->next;
  43.             if (pp)
  44.                 pp->next = pl;
  45.             else
  46.                 players = pl;
  47.             break;
  48.         }
  49.     }
  50.  
  51.     DEL0 (player);
  52.  
  53.     return (pl);
  54. }
  55.  
  56. extern void FAR
  57. players_delete (void)
  58. {
  59.     PLAYER    *pl;
  60.  
  61.     for (pl = players; pl;)
  62.         pl = DEL (pl);
  63.     players = 0;
  64. }
  65.  
  66. /* Remove all objects of a remote player from our world.
  67. */
  68. extern void FAR
  69. player_remove (PLAYER *player)
  70. {
  71.     OBJECT    *p;
  72.  
  73.     for (p = CO; p; p = p->next) {
  74.         if ((p->flags & F_IMPORTED) && p->rplayer == player)
  75.             p->flags |= F_DEL|F_MOD;
  76.     }
  77.     if (player->flags & PL_PLAYING) {
  78.         player->flags &= ~PL_PLAYING;
  79.         player->flags |= PL_ACTIVE;
  80.         netport_count (player, -1);
  81.     }
  82. }
  83.  
  84. /* Remove all (or just team) imported objects from our world.
  85. */
  86. extern void FAR
  87. players_remove (PLAYER *ptype)
  88. {
  89.     PLAYER    *pl;
  90.  
  91.     for (pl = players; pl; pl = pl->next) {
  92.         if (ptype == st.all_team && stricmp (pl->team, st.teamname))
  93.             continue;
  94.         player_remove (pl);
  95.     }
  96. }
  97.  
  98. /* Delete all noise-messages from a player.
  99. */
  100. LOCAL_FUNC void NEAR
  101. player_flush (PLAYER *pl)
  102. {
  103.     PACKET    *pack;
  104.  
  105.     for (pack = pl->incoming; pack;) {
  106.         pack = packet_del (pack);
  107.         if (pl->flags & PL_PLAYING)
  108.             ++STATS_NETERRL;
  109.         else
  110.             ++STATS_NETERRNOISE;
  111.     }
  112.     pl->incoming =  0;
  113.     pl->tail = 0;
  114. }
  115.  
  116. /* Delete all noise-messages.
  117. */
  118. extern void FAR
  119. players_flush (void)
  120. {
  121.     PLAYER    *pl;
  122.  
  123.     for (pl = players; pl; pl = pl->next)
  124.         player_flush (pl);
  125. }
  126.  
  127. /* purge silent players (life was never fair).
  128. */
  129. extern void FAR
  130. players_purge (void)
  131. {
  132.     PLAYER    *pl;
  133.  
  134.     for (pl = players; pl; pl = pl->next) {
  135.         if (!(pl->flags & (PL_NOTIDLE & ~PL_ACTIVE)))
  136.             continue;
  137.         if (pl->timeout < st.present) {
  138.             LogPrintf ("%s ", Tm->Ctime ());
  139.             MsgWPrintf (-100, "Timed: %s:%s", pl->name, pl->team);
  140.             if (pl->flags & PL_PLAYING)
  141.                 remote_noplay (pl);
  142.             player_flush (pl);
  143.             player_remove (pl);
  144.             pl->flags &= ~PL_NOTIDLE;
  145.         }
  146.     }
  147. }
  148.  
  149. /* Find a player by address.
  150. */
  151. extern PLAYER * FAR
  152. player_find (PACKET *pack)
  153. {
  154.     PLAYER    *pl;
  155.  
  156.     for (pl = players; pl; pl = pl->next)
  157.         if (pl->netport == pack->netport &&
  158.             !memcmp (pl->address, pack->address, LADDRESS))
  159.             break;
  160.     return (pl);
  161. }
  162.  
  163. /* Make sure a player is registered.
  164. */
  165. extern PLAYER * FAR
  166. player_active (PACKET *pack)
  167. {
  168.     PLAYER    *pl;
  169.  
  170.     if (!(pl = player_find (pack)))
  171.         pl = player_add (pack);
  172.     if (pl && !(pl->flags & PL_NOTIDLE))
  173.         pl->flags |= PL_ACTIVE;
  174.     return (pl);
  175. }
  176.  
  177. /* Provide access to players list.
  178. */
  179. extern PLAYER * FAR
  180. player_next (PLAYER *pl)
  181. {
  182.     if (pl)
  183.         return (pl->next);
  184.     else
  185.         return (players);
  186. }
  187.  
  188. extern int FAR
  189. players_init (void)
  190. {
  191.     players = 0;
  192.     return (0);
  193. }
  194.  
  195. extern void FAR
  196. players_term (void)
  197. {
  198.     players_remove (st.all_known);
  199.     players_delete ();
  200. }
  201.